package com.shujujiegou.队列.java模拟单向队列实现;

public class MyQueue {
    private Object[] queArray;
    // 队列总大小
    private int maxSize;
    // 前端
    private int front;
    // 后端
    private int rear;
    // 队列总的大小
    private int nItems;

    public MyQueue(int maxSize) {
        this.maxSize = maxSize;
        this.queArray = new Object[maxSize];
        this.front = 0;
        this.rear = -1;
        this.nItems = 0;
    }

    //队列中新增数据
    private void insert(Object value) {
        if(isFull()) {
            System.out.println("队列中的数据已经满了！");
            return;
        } else {
            // 如果队列尾部指向顶了， 执行队列的第一个元素
            if(this.rear == (maxSize - 1)) {
                this.rear = -1;
            }

            this.queArray[++this.rear] = value;
            nItems ++;
        }
    }

    //移除数据
    public Object remove() {
        if(isEmpty()) {
            System.out.println("队列为空不能进行删除操作");
            return null;
        } else {
            Object item = this.queArray[this.front];
            this.front++;
            if(this.front == this.maxSize) {
                this.front = 0;
            }

            this.nItems--;
            return item;
        }
    }

    //查看对头数据
    public Object peekFront() {
        return this.queArray[this.front];
    }

    //判断队列是否满了
    public Boolean isFull() {
        if(this.nItems == this.maxSize) {
            return true;
        }

        return false;
    }

    //判断队列是否为空
    private Boolean isEmpty() {
        if(this.nItems == 0) {
            return true;
        }

        return false;
    }

    //返回队列的大小
    public int getSize() {
        return this.nItems;
    }
}
